package com.cskaoyan.service;

import com.cskaoyan.mapper.AccountMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author stone
 * @date 2022/11/14 10:52
 */
@Service
public class AccountServiceImpl implements AccountService{

    @Autowired
    AccountMapper accountMapper;



    //@Transactional // TransactionDefinition如果没有配置，采用的就是默认值
    //@Transactional(readOnly = true)
    //@Transactional(timeout = 5)
    @Transactional(noRollbackFor = {ArithmeticException.class}) // 小吴提交了，小芳没执行到
    //@Transactional(noRollbackForClassName = "java.lang.ArithmeticException")
    @Override
    public void transfer(Integer fromId, Integer destId, Integer money) {
        // 查询转出、接收方的钱
        Integer fromMoney = accountMapper.select(fromId);
        Integer destMoney = accountMapper.select(destId);

        // 计算转账成功，他两应该有多少钱
        fromMoney -= money;
        destMoney += money;

        // 根据id更新money
        accountMapper.update(fromId, fromMoney);

        int i = 1 / 0;

        accountMapper.update(destId, destMoney);//run
    }

    @Override
    public void method2() {


    }


}
